/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package interfaces;

import input.IRCCommand;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import output.IRCOutput;

public abstract class CommandHandler {
   /**
    * Returns a method that can handle the specified command. Falls back on the general handle() method if no direct match is
    * found
    * 
    * @param name
    *           Name of the command to handle
    * @param params
    *           Parameters for the method to handle
    * @return a method that can handle the specified command. Falls back on the general handle() method if no direct match is
    *         found
    */
   public Method getHandlerMethod(final String name, @SuppressWarnings("rawtypes") final Class[] params) {
	  try {
		 return getClass().getDeclaredMethod(name, params);
	  } catch (SecurityException e) {
		 Logger.getLogger("Error").error("", e);
	  } catch (NoSuchMethodException e) {
		 // don't log this, as it might be valid
	  }
	  try {
		 return getClass().getDeclaredMethod("handle", params);
	  } catch (SecurityException e) {
		 Logger.getLogger("Error").error("", e);
	  } catch (NoSuchMethodException e) {
		 Logger.getLogger("Error").error("", e);
	  }
	  return null;
   }

   /**
    * Used to handle dynamic commands and such that can't be hard coded
    * 
    * @param command
    *           The issued command
    * @param channel
    *           The channel the command was issued in
    * @param sender
    *           The user that issued the command
    * @param output
    *           The irc output object
    */
   public abstract void handle(IRCCommand command, IRCOutput output);
}
